package com.xinqi.modules.exam.analyze.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.exam.analyze.convert.CourseExamPaperExplainConvert;
import com.xinqi.modules.exam.analyze.domain.CourseExamPaperExplainEntity;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamPaperExplainCreateDTO;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamPaperExplainDeleteDTO;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamPaperExplainQueryDTO;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamPaperExplainUpdateDTO;
import com.xinqi.modules.exam.analyze.dto.rsp.CourseExamPaperExplainResultDTO;
import com.xinqi.modules.exam.analyze.manager.CourseExamPaperExplainManager;
import com.xinqi.modules.exam.analyze.service.CourseExamPaperExplainService;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 课程测评讲解服务层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Validated
@Service("courseExamPaperExplainService")
@RequiredArgsConstructor
public class CourseExamPaperExplainServiceImpl implements CourseExamPaperExplainService {
    private final CourseExamPaperExplainConvert courseExamPaperExplainConvert;
    private final CourseExamPaperExplainManager courseExamPaperExplainManager;
    private final CourseExamPaperManager courseExamPaperManager;

    /**
     * 分页查询 课程测评讲解数据
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseExamPaperExplainResultDTO> page(Param pageable, CourseExamPaperExplainQueryDTO query) {
        IPage<CourseExamPaperExplainEntity> page = courseExamPaperExplainManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, courseExamPaperExplainConvert::convert);
    }

    /**
     * 查询 课程测评讲解数据
     */
    @Override
    public List<CourseExamPaperExplainResultDTO> find(CourseExamPaperExplainQueryDTO query) {
        return courseExamPaperExplainManager.list(wrapper(query)).stream()
            .map(courseExamPaperExplainConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CourseExamPaperExplainEntity> wrapper(CourseExamPaperExplainQueryDTO query) {
        LambdaQueryWrapper<CourseExamPaperExplainEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(CourseExamPaperExplainEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 课程测评讲解数据
     */
    @Override
    public CourseExamPaperExplainResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(courseExamPaperExplainManager.findById(id)).map(courseExamPaperExplainConvert::convert).orElse(null);
    }

    /**
     * 新增 课程测评讲解数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid CourseExamPaperExplainCreateDTO dto) {
        CourseExamPaperExplainEntity entity = courseExamPaperExplainConvert.create(dto);
        courseExamPaperExplainManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 课程测评讲解数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid CourseExamPaperExplainUpdateDTO dto) {
        Optional.ofNullable(courseExamPaperExplainManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        CourseExamPaperExplainEntity entity = courseExamPaperExplainConvert.update(dto);
        return courseExamPaperExplainManager.updateById(entity);
    }

    /**
     * 根据id删除 课程测评讲解数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return courseExamPaperExplainManager.removeById(id);
    }

    /**
     * 根据id删除 课程测评讲解数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(CourseExamPaperExplainDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
